/*
 * See documentation at https://nRF24.github.io/RF24
 * See License information at root directory of this library
 * Author: Brendan Doherty (2bndy5)
 */

/**
 * A simple example of sending data from 1 nRF24L01 transceiver to another.
 *
 * This example was written to be used on 2 devices acting as "nodes".
 * Use the Serial Monitor to change each node's behavior.
 */
#include <SPI.h>
#include "printf.h"
#include "RF24.h"

#include <dht11.h> //引用dht11库文件，使得下面可以调用相关参数
#define DHT11PIN 2 //定义温湿度针脚号为2号引脚
dht11 DHT11;       //实例化一个对象
#define send_size 50

// instantiate an object for the nRF24L01 transceiver
RF24 radio(7, 8); // using pin 7 for the CE pin, and pin 8 for the CSN pin

// Let these addresses be used for the pair
uint8_t address[][6] = {"1Node", "2Node"};
// It is very helpful to think of an address as a path instead of as
// an identifying device destination

// to use different addresses on a pair of radios, we need a variable to
// uniquely identify which address this radio will use to transmit
bool radioNumber = 0; // 0 uses address[0] to transmit, 1 uses address[1] to transmit

// Used to control whether this node is sending or receiving
bool role = true; // true = TX role, false = RX role

// For this example, we'll be using a payload containing
// a single float number that will be incremented
// on every successful transmission
char payload[send_size];

void setup()
{
  pinMode(DHT11PIN, OUTPUT); //定义输出口
  Serial.begin(115200);
  while (!Serial)
  {
    // some boards need to wait to ensure access to serial over USB
  }
  //
  // initialize the transceiver on the SPI bus
  if (!radio.begin())
  {
    Serial.println(F("radio hardware is not responding!!"));
    while (1)
    {
    } // hold in infinite loop
  }

  //  // print example's introductory prompt
  //  Serial.println(F("RF24/examples/GettingStarted"));
  //
  //  // To set the radioNumber via the Serial monitor on startup
  //  Serial.println(F("Which radio is this? Enter '0' or '1'. Defaults to '0'"));
  //  while (!Serial.available()) {
  //    // wait for user input
  //  }
  //  char input = Serial.parseInt();
  //  radioNumber = input == 1;
  //  Serial.print(F("radioNumber = "));
  //  Serial.println((int)radioNumber);
  //
  //  // role variable is hardcoded to RX behavior, inform the user of this
  //  Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));

  // Set the PA Level low to try preventing power supply related problems
  // because these examples are likely run with nodes in close proximity to
  // each other.
  radio.setPALevel(RF24_PA_LOW); // RF24_PA_MAX is default.

  // save on transmission time by setting the radio to only transmit the
  // number of bytes we need to transmit a float
  radio.setPayloadSize(send_size); // float datatype occupies 4 bytes

  // set the TX address of the RX node into the TX pipe
  radio.openWritingPipe(address[radioNumber]); // always uses pipe 0

  // set the RX address of the TX node into a RX pipe
  radio.openReadingPipe(1, address[!radioNumber]); // using pipe 1

  // additional setup specific to the node's role
  if (role)
  {
    radio.stopListening(); // put radio in TX mode
  }
  else
  {
    radio.startListening(); // put radio in RX mode
  }

  // For debugging info
  // printf_begin();             // needed only once for printing details
  // radio.printDetails();       // (smaller) function that prints raw register values
  // radio.printPrettyDetails(); // (larger) function that prints human readable data

} // setup
float tem = 0.0;
float hum = 0.0;
int count = 0.0;
int temp1;
int temp2;
char temp_str[10];
char temp_str2[10];

void loop()
{
  int chk = DHT11.read(DHT11PIN);   //将读取到的值赋给chk
  temp1 = (float)DHT11.temperature; //将温度值赋值给tem
  temp2 = (float)DHT11.humidity;    //将湿度值赋给hum
  tem += temp1;
  hum += temp2;
  if (++count >= 3)
  {
    tem /= 3.0;
    hum /= 3.0;
    count = 0;
    if (role)
    {
      // This device is a TX node
      unsigned long start_timer = micros();           // start the timer
      bool report = radio.write(&payload, send_size); // transmit & save the report
      unsigned long end_timer = micros();             // end the timer
      if (report)
      {
        Serial.print(F("Transmission successful! ")); // payload was delivered
        Serial.print(F("Time to transmit = "));
        Serial.print(end_timer - start_timer); // print the timer result
        Serial.print(F(" us. Sent: "));
        dtostrf(tem, 2, 2, temp_str);
        dtostrf(hum, 2, 2, temp_str2);
        sprintf(payload, "dht1-->tem:%s hum:%s", temp_str, temp_str2);
        Serial.println(payload); // print payload sent
        Serial.println(sizeof(payload));
      }
      else
      {
        Serial.println(F("Transmission failed or timed out")); // payload was not delivered
      }
    }
    tem = 0;
    hum = 0;
  }
  // role

  delay(1000); // slow transmissions down by 1 second
} // loop
